﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;

namespace Refix.RepositoryBrowser.Models
{
    public class RepositoryModel : IRepositoryModel
    {
        public event EventHandler RepositoryChanged;
        public event EventHandler<RepositoryItemEventArgs> RepositoryItemAdded;
        public event EventHandler<RepositoryItemEventArgs> RepositoryItemRemoved;
        public event EventHandler<RepositoryItemEventArgs> RepositoryItemSelected;
        public event EventHandler<ValueEventArgs<string>> StatusReported;
        public event EventHandler<ValueEventArgs<string>> ErrorReported;

        public RepositoryModel()
        {
            RepositoryChanged += (o, e) => { };
            RepositoryItemAdded += (o, e) => { };
            RepositoryItemRemoved += (o, e) => { };
            RepositoryItemSelected += (o, e) => { };
            StatusReported += (o, e) => { };
            ErrorReported += (o, e) => { };
        }

        private RepositoryItem _repository;
        private RepositoryItem _selectedItem;

        public RepositoryItem Repository
        {
            get { return _repository; }
            set
            {
                _repository = value;
                RepositoryChanged(this, EventArgs.Empty);
            }
        }

        public RepositoryItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                RepositoryItemSelected(this, new RepositoryItemEventArgs(value));
            }
        }

        public void AddRepositoryItem(RepositoryItem parent, RepositoryItem child)
        {
            parent.Children.Add(child);
            child.Parent = parent;
            RepositoryItemAdded(this, new RepositoryItemEventArgs(child));
        }

        public void RemoveRepositoryItem(RepositoryItem parent, RepositoryItem child)
        {
            parent.Children.Remove(child);
            RepositoryItemRemoved(this, new RepositoryItemEventArgs(child));
        }

        public void ReportStatus(string status)
        {
            StatusReported(this, new ValueEventArgs<string>(status));
        }

        public void ReportError(string message)
        {
            ErrorReported(this, new ValueEventArgs<string>(message));
        }
    }
}